package cn.nubia.admin.application.service;

import cn.nubia.admin.application.bean.AdItem;
import cn.nubia.admin.application.bean.AdSpace;
import cn.nubia.admin.application.dto.AdItemDTO;
import cn.nubia.admin.application.query.AdItemQuery;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.BeanUtils;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cnJason on 2015/7/6.
 * 广告内容Service操作
 */
@Service
public class AdItemService extends BaseServiceImpl<AdItem> {

    @Autowired
    private AdSpaceService adSpaceService;
    @Autowired
    SystemLogService systemLogService;

    /**
     * 通过广告位id获取广告数量.
     *
     * @param id 广告位id
     * @return 广告数量.
     */
    public Integer countBySpaceId(Integer id) {
        Query<AdItem> query = Query.build(AdItem.class);
        query.addEq("adSpaceId", id);
        return count(query);
    }

    public Paged<AdItemDTO> findPage(AdItemQuery adItemQuery) {

        //通过广告位名称获取广告位信息
        List<AdSpace> adSpacelist = adSpaceService.getAdSpaceByName(adItemQuery.getAdSpaceName());

        //查询广告内容
        Query<AdItem> query = Query.build(AdItem.class);
        List<Integer> spaceIds = new ArrayList<Integer>();
        for (AdSpace adSpace : adSpacelist) {
            spaceIds.add(adSpace.getId());
        }
        query.addLike("adItemTitle", adItemQuery.getAdItemTitle());
        query.addIn("adSpaceId", spaceIds);
        query.addIn(AdItem.COLUMN_STATE, AdItem.STATE_SHOW, AdItem.STATE_HIDDEN);
        query.setPaged(adItemQuery.getPageNo(), adItemQuery.getPageSize());
        query.addOrder("createTime", Query.DBOrder.DESC);
        Paged<AdItem> adItemPaged = findPagedByQuery(query);

        List<AdItem> list = adItemPaged.getListData();

        Paged<AdItemDTO> adItemDTOPaged = new Paged<AdItemDTO>();
        adItemDTOPaged.setPageSize(adItemPaged.getPageSize());
        adItemDTOPaged.setPageNo(adItemPaged.getPageNo());
        adItemDTOPaged.setTotalCount(adItemPaged.getTotalCount());
        List<AdItemDTO> adItemDTOList = new ArrayList<AdItemDTO>();

        for (AdItem adItem : list) {
            AdItemDTO adItemDTO = new AdItemDTO();

            BeanUtils.copyProperties(adItem, adItemDTO);
            Integer adSpaceId = adItem.getAdSpaceId();
            AdSpace adSpace = adSpaceService.get(adSpaceId);
            if (adSpace != null) {
                adItemDTO.setAdSpaceName(adSpace.getAdSpaceName());
            }
            adItemDTOList.add(adItemDTO);
        }
        adItemDTOPaged.setListData(adItemDTOList);
        return adItemDTOPaged;
    }

    public String save(AdItem adItem) {
        AdSpace adSpace = adSpaceService.get(adItem.getAdSpaceId());
        Query<AdItem> adItemQuery = Query.build(AdItem.class);
        adItemQuery.addEq("adSpaceId",adItem.getAdSpaceId());
        int count = count(adItemQuery);

        if(adSpace.getAdLimit() != null && count == adSpace.getAdLimit()){
            return "添加广告超过限制";
        }


        Integer currTime = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        adItem.setLastModifyTime(currTime);
        if (null == adItem.getId()) {
            adItem.setCreateTime(currTime);
            adItem.setCreateAdminId(adminId);
            adItem.setCreateAdminName(adminName);
            insert(adItem);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.ADITEMLIST, SystemLog.OperateTypeEnum.ADD, adItem.getId());
        } else {
            AdItem domain = get(adItem.getId());
            adItem.setCreateAdminName(domain.getCreateAdminName());
            adItem.setCreateTime(domain.getCreateTime());
            adItem.setCreateAdminId(domain.getCreateAdminId());
            adItem.setModifyAdminId(adminId);
            adItem.setModifyAdminName(adminName);
            update(adItem);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.ADITEMLIST, SystemLog.OperateTypeEnum.EDIT, adItem.getId());
        }
        return "";
    }

    public void changeAdItem(Integer adItemId,Integer state) {
        AdItem adItem = get(adItemId);
        adItem.setState(state);
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        adItem.setModifyAdminName(adminName);
        adItem.setModifyAdminId(adminId);
        adItem.setLastModifyTime(curr);
        update(adItem);

        //log
        if(state == AdItem.STATE_DEL){
            systemLogService.log(SystemLog.ModuleTypeEnum.ADITEMLIST, SystemLog.OperateTypeEnum.DEL, adItem.getId());
        }else if(state == AdItem.STATE_HIDDEN){
            systemLogService.log(SystemLog.ModuleTypeEnum.ADITEMLIST, SystemLog.OperateTypeEnum.HIDDEN, adItem.getId());
        }else if(state == AdItem.STATE_SHOW){
            systemLogService.log(SystemLog.ModuleTypeEnum.ADITEMLIST, SystemLog.OperateTypeEnum.SHOW, adItem.getId());
        }
    }
}
